<?php

class Profile_WidgetController extends Zend_Controller_Action
{
	protected $_entClassIdName = NULL;
	protected $_entId = NULL;
	protected $_entIdName = NULL;
	protected $_entObj = NULL;


	public function init()
	{
		parent::init();

		$this->_entClassIdName = $this->_getParam('entity_class');
		$this->_entId = $this->_getParam('entity_id');
		if (empty($this->_entClassIdName) === FALSE || intval($this->_entId) > 0) {
			$this->_entObj = Euso_Object::getInstance($this->_entClassIdName, $this->_entId);
			if ($this->_entObj != NULL) {
				$this->_entIdName = $this->_entObj->getIdName();
			}
		}

		$pageAction = $this->_getParam('page_param1');

		if (empty($pageAction) === FALSE) {
			$this->_forward($pageAction);
		}
	}


	public function indexAction()
	{
		if ($this->_entObj != NULL) {
			$profile = Profile_Model_Data::getInstance($this->_entClassIdName, $this->_entId);
			if ($profile != NULL) {
				$dataArray = $profile->getDataArray();
			}
		}

		$fields = array();
		$fields['group'] = array(
			'about' => 'About Us',
			'location' => 'Location',
			'website' => 'Website',
		);
		$fields['user'] = array(
			'name' => 'Name',
			'about' => 'About Me',
			'location' => 'Location',
			'website' => 'Website',
			'interests' => 'Interests',
			'occupation' => 'Occupation',
		);

		//HACK: this should be automated process
		if (is_array($dataArray) && count($dataArray) > 0) {
			$this->view->profileData = array();
			foreach ($dataArray as $key => $value) {
				if (empty($value) === FALSE && empty($fields[$this->_entClassIdName][$key]) === FALSE) {
					$this->view->profileData[$fields[$this->_entClassIdName][$key]] = $value;
				}
			}
		}

		if ($this->_getParam('widget_display_mode') === 'full') {
			$sessAcc = Account_Model_Session::getAccount();
			if ($sessAcc != NULL) {
				if ($this->_entClassIdName === 'user') {
					if ($sessAcc->getId() == $this->_entId) {
						$this->view->editLinkVisible = TRUE;
						$this->view->editInfoUrl = SITE_URL_BASE . $this->getRequest()->getPathInfo() .'/edit';
						$this->view->editAvatarUrl = SITE_URL_BASE . $this->getRequest()->getPathInfo() .'/editavatar';
					}
				} else if ($this->_entClassIdName === 'group') {
					$groupMember = Group_Model_Member::groupGetMember($this->_entObj->getId(), $sessAcc->getId(), TRUE);
					if ($groupMember != NULL && $groupMember->hasRole('admin')) {
						$this->view->editLinkVisible = TRUE;
						$this->view->editInfoUrl = SITE_URL_BASE . $this->getRequest()->getPathInfo() .'/edit';
						$this->view->editAvatarUrl = SITE_URL_BASE . $this->getRequest()->getPathInfo() .'/editavatar';
					}
				}
			}
		}
	}

	public function editAction()
	{
		$request = $this->getRequest();
		if ($this->_entClassIdName === 'group') {
			$sessAcc = Account_Model_Session::getAccount();
			if ($sessAcc == NULL) {
				throw new Zend_Controller_Action_Exception('Forbidden', 403);
			}
			$groupMember = Group_Model_Member::groupGetMember($this->_entObj->getId(), $sessAcc->getId(), TRUE);
			if ($groupMember == NULL && $groupMember->hasRole('admin') === FALSE) {
				throw new Zend_Controller_Action_Exception('Forbidden', 403);
			}

			$form = new Profile_Form_EditGroup();
			$profile = Profile_Model_Data::getInstance($this->_entClassIdName, $this->_entId);

			// Check to see if this action has been POST'ed to.
			if ($request->isPost()) {

				// Now check to see if the form submitted exists, and
				// if the values passed in are valid for this form.
				if ($form->isValid($request->getPost())) {

					$values = $form->getValues();

					//TODO!
					if (isset($values['profile_tagline'])) {
						$profile->setData('tagline', $values['profile_tagline']);
					}
					if (isset($values['profile_about'])) {
						$profile->setData('about', $values['profile_about']);
					}
					if (isset($values['profile_location'])) {
						$profile->setData('location', $values['profile_location']);
					}
					if (isset($values['profile_website'])) {
						$profile->setData('website', $values['profile_website']);
					}

					$this->_redirect(dirname($this->getRequest()->getPathInfo()));
				}

			} else if ($this->_entObj != NULL) {
				if ($profile != NULL) {
					$dataArray = $profile->getDataArray();
				}

				if (is_array($dataArray)) {
					$form->profile_tagline->setValue($dataArray['tagline']);
					$form->profile_about->setValue($dataArray['about']);
					$form->profile_location->setValue($dataArray['location']);
					$form->profile_website->setValue($dataArray['website']);
				}
			}

		} else if ($this->_entClassIdName === 'user') {

			$form = new Profile_Form_Edit();
			$profile = Profile_Model_Data::getInstance($this->_entClassIdName, $this->_entId);

			// Check to see if this action has been POST'ed to.
			if ($request->isPost()) {

				// Now check to see if the form submitted exists, and
				// if the values passed in are valid for this form.
				if ($form->isValid($request->getPost())) {

					$values = $form->getValues();

					//TODO!
					if (isset($values['profile_name'])) {
						$profile->setData('name', $values['profile_name']);
					}
					if (isset($values['profile_about'])) {
						$profile->setData('about', $values['profile_about']);
					}
					if (isset($values['profile_location'])) {
						$profile->setData('location', $values['profile_location']);
					}
					if (isset($values['profile_website'])) {
						$profile->setData('website', $values['profile_website']);
					}
					if (isset($values['profile_interests'])) {
						$profile->setData('interests', $values['profile_interests']);
					}
					if (isset($values['profile_occupation'])) {
						$profile->setData('occupation', $values['profile_occupation']);
					}

					$this->_redirect(dirname($this->getRequest()->getPathInfo()));
				}

			} else if ($this->_entObj != NULL) {
				if ($profile != NULL) {
					$dataArray = $profile->getDataArray();
				}

				if (is_array($dataArray)) {
					$form->profile_name->setValue($dataArray['name']);
					$form->profile_about->setValue($dataArray['about']);
					$form->profile_location->setValue($dataArray['location']);
					$form->profile_website->setValue($dataArray['website']);
					$form->profile_interests->setValue($dataArray['interests']);
					$form->profile_occupation->setValue($dataArray['occupation']);
				}
			}

		} else {
			$form = '';
		}

		$this->view->form = $form;
	}

	public function editavatarAction()
	{
		$request = $this->getRequest();
	//	$upload = new Zend_File_Transfer();

		$form = new Profile_Form_EditAvatar();
		if ($request->isPost()) {

			if ($form->isValid($request->getPost()) === TRUE) {

				$file_basename = $_FILES['profile_avatar']['name'];
				$img_fname_src = $_FILES['profile_avatar']['tmp_name'];

				$pos = strrpos($file_basename, '.');
				if ($pos === false) { // dot is not found in the filename
					$img_fname = $file_basename;
					$fext = ''; // no extension
					$fdext = '';
				} else {
					$img_fname = substr($file_basename, 0, $pos);
					$fext = strtolower( substr( $file_basename, $pos+1));

					// unify extension for JPEG files
					if ($fext == 'jpeg' || $fext == 'jpe') {
						$fext = 'jpg';
					}

					$fdext = '.' . $fext;
				}

				$is_image = ($fext == 'jpg' || $fext == 'png' || $fext == 'gif');

				$image = NULL;

				if ($fext == 'png') {
					$image = imagecreatefrompng($img_fname_src);
				} else if ($fext == 'gif') {
					$image = imagecreatefromgif($img_fname_src);
				} else if ($fext == 'jpg') {
					$image = imagecreatefromjpeg($img_fname_src);
				}

				if ($image != false) {
					$img_w = imagesx($image);
					$img_h = imagesy($image);

					$dst_w = 50;
					$dst_h = 50;

					$thumbSizes = array(48, 96, 192);

					foreach ($thumbSizes as $ts) {

						$img_fname_dst = FILES_PATH .'/avatar/'. $this->_entClassIdName .'-'. $this->_entIdName .'-'. $ts .'.png';

						$dst_w = $ts;
						$dst_h = $ts;

						if ($img_w > $dst_w || $img_h > $dst_h) {

							if ($img_w > $img_h) {
								$off_x = ($img_w - $img_h) / 2;
								$off_y = 0;
								$src_w = $img_h;
								$src_h = $img_h;
							} else {
								$off_x = 0;
								$off_y = ($img_h - $img_w) / 2;
								$src_w = $img_w;
								$src_h = $img_w;
							}

							$tn_image = imagecreatetruecolor($dst_w, $dst_h);
							imagecopyresampled($tn_image, $image, 0, 0, $off_x, $off_y,
								$dst_w, $dst_h, $src_w, $src_h);

							imagepng($tn_image, $img_fname_dst, 9);

							imagedestroy($tn_image);

						} else {
							imagepng($image, $img_fname_dst, 9);
						}
					}

					$this->_redirect($this->getRequest()->getPathInfo());

				} else {
					//TODO: show message
				}
			}
		}


		$this->view->form = $form;
	}
}
